package studentinformationmanagementsystem.servlet.student;


import com.google.gson.Gson;
import studentinformationmanagementsystem.entity.Page;
import studentinformationmanagementsystem.entity.Student;
import studentinformationmanagementsystem.entity.TacShow;
import studentinformationmanagementsystem.service.ClassandgradeService;
import studentinformationmanagementsystem.service.StudentService;
import studentinformationmanagementsystem.service.TakeACourseService;
import studentinformationmanagementsystem.service.TemporaryClassTableService;
import studentinformationmanagementsystem.service.impl.ClassandgradeServicelmpl;
import studentinformationmanagementsystem.service.impl.StudenServiceImpl;
import studentinformationmanagementsystem.service.impl.TakeACourseServiceImpl;
import studentinformationmanagementsystem.service.impl.TemporaryClassTableServiceImpl;
import studentinformationmanagementsystem.servlet.BaseServlet;
import studentinformationmanagementsystem.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static studentinformationmanagementsystem.entity.Page.DEFAULT_PAGE_SIZE;

/**
 * @author 苗庆
 * @version 1.0
 */

@WebServlet("/student1/StudentServlet.do")
public class StudentServlet extends BaseServlet {

    ClassandgradeService classandgradeService=new ClassandgradeServicelmpl();
    TemporaryClassTableService temporaryClassTableService=new TemporaryClassTableServiceImpl();
    TakeACourseService takeACourseService=new TakeACourseServiceImpl();
    StudentService studentService=new StudenServiceImpl();


    //获取学生的班级信息
    public void studenClass(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
         //获取现在登陆的学生信息
        Student studentlogin = (Student)request.getSession().getAttribute("Studentlogin");

        if (studentlogin!=null){

            //调用获取班级信息方法
            List<Object> list = classandgradeService.getClass(studentlogin);

            //基于JSP 将班级信息返回给前端页面
            request.setAttribute("page", list);
            request.getRequestDispatcher("/student/function/myinfo/ClassInfo.aspx.jsp").forward(request, response);
        }
    }


    //展示所有选课
    public void pageTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), DEFAULT_PAGE_SIZE);
        // 调用 temporaryClassTableService 中的 pageTeacher
        Page<TacShow> tacShowPage = temporaryClassTableService.pageTeacher(pageNo, pageSize);
        //基于 JSP
        request.setAttribute("page", tacShowPage);
        request.getRequestDispatcher("/student/function/educationcenter/Application.aspx.jsp").forward(request, response);
    }


    //学生选课
    public void addCourse (HttpServletRequest request, HttpServletResponse response) throws IOException {
        String number = request.getParameter("number");
        Student studentlogin = (Student)request.getSession().getAttribute("Studentlogin");
        Map<String, String> map = takeACourseService.isAddCourse(number, studentlogin);



        if (map.get("code").equals("0")){  //课程重复，选课失败

            response.getWriter().write("0");

        }else if(map.get("code").equals("1")){//选课成功

            response.getWriter().write("1");

        }else if (map.get("code").equals("3")){  //选课人数已满，选课失败

            response.getWriter().write("3");
        } else{//选课失败

            response.getWriter().write("2");

        }

    }


    //展示学生个人选课信息
    public void lookCourse (HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取现在登陆的学生信息
        Student studentlogin = (Student)request.getSession().getAttribute("Studentlogin");

        //当有学生的时候，去查询学生的个人选课信息
        if (studentlogin!=null){
            List<TacShow> tacShows = takeACourseService.lookCourse(studentlogin);

            //如果没有查询到这个学生的选课信息就返回1
            if (tacShows.size()==0 || tacShows==null){

                //这里如果是返回数字的话前端就会接收不到，返回字符串就可以不知道为什么
                response.getWriter().write("1");
                //找到了就将list对象tacShows返回给前端页面展示

            }else {

                Gson gson = new Gson();
                String studentsStr = gson.toJson(tacShows);
                response.getWriter().write(studentsStr);
                
            }
        }
    }



    //学生退课
    public void deleteCourse (HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取课程编号
        String number = request.getParameter("number");
        //获取现在登陆的学生信息
        Student studentlogin = (Student)request.getSession().getAttribute("Studentlogin");

        boolean deleteCourse = takeACourseService.isDeleteCourse(number,studentlogin);

        if (deleteCourse){ //退课成功

            response.getWriter().write("1");

        }else {  //退课失败

            response.getWriter().write("2");

        }

    }


    //学生修改账号密码
    public void changePassword (HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取现在登陆的学生信息
        Student studentlogin = (Student)request.getSession().getAttribute("Studentlogin");

        //旧密码
        String formerpassword=request.getParameter("formerpassword");


         //新密码
         String newpassword=request.getParameter("newpassword");


        //确认的新密码
        String confirmpassword=request.getParameter("confirmpassword");

        Map<String, String> map = studentService.changePassword(studentlogin,formerpassword,newpassword,confirmpassword);


        if (map.get("code").equals("0")){  //"输入的旧密码不正确"

            response.getWriter().write("0");

        }else if(map.get("code").equals("1")){  //"新密码两次输入的不一致"

            response.getWriter().write("1");

        }else if (map.get("code").equals("2")){  //"修改密码成功"

            response.getWriter().write("2");

        }else {          //修改密码失败

            response.getWriter().write("3");

        }

    }





}
